Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

遞迴 費氏數列詳解

遞迴 費氏數列詳解

原子習慣:基本原理 - 為何細微改變會帶來巨大差異?

原子習慣:基本原理 - 為何細微改變會帶來巨大差異?

Intigriti 七月份 XSS 挑戰:突破層層關卡

Intigriti 七月份 XSS 挑戰:突破層層關卡






留言討論